package com.aoxun.android.gridmanager.common;

import com.socks.library.KLog;
import com.aoxun.android.commons.base.BaseActivity;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * 没有map时使用的转换器
 * @param <T>
 */
public class LoadingTransformer<T> implements ObservableTransformer<T, T> {
        //private static final String TAG = "LoadingTransformer";
        private Disposable mDisposable;
        private WeakReference<BaseActivity> baseActivity;

        public LoadingTransformer(BaseActivity baseActivity) {
            this.baseActivity = new WeakReference<BaseActivity>(baseActivity);
        }

        @Override
        public ObservableSource<T> apply(@NonNull Observable<T> upStream) {
            Observable.just(1).delay(500, TimeUnit.MILLISECONDS)//延时500毫秒
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnDispose(new Action() {
                        @Override
                        public void run() throws Exception {
                            if(baseActivity!=null && baseActivity.get()!=null) {
                                baseActivity.get().getUiDelegate().dismissLoadingDlg();
                            }
                            //KLog.d("doOnDispose------>");
                        }
                    })
                    .subscribe(new Observer<Integer>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable disposable) {
                            mDisposable = disposable;
                        }

                        @Override
                        public void onNext(@NonNull Integer integer) {
                            if(baseActivity!=null && baseActivity.get()!=null) {
                                baseActivity.get().getUiDelegate().showLoadingDlg();
                            }
                            KLog.d("start event------>");
                        }

                        @Override
                        public void onError(@NonNull Throwable throwable) {}
                        @Override
                        public void onComplete() {}
                    });
            return upStream.doOnNext(new Consumer<T>() {
                @Override
                public void accept(@NonNull T t) throws Exception {
                    if (!mDisposable.isDisposed()) {
                        mDisposable.dispose();
                    }
                }
            }).observeOn(AndroidSchedulers.mainThread())
                    .doOnTerminate(new Action() {
                        @Override
                        public void run() throws Exception {
                            if(baseActivity!=null && baseActivity.get()!=null) {
                                baseActivity.get().getUiDelegate().dismissLoadingDlg();
                            }
                            KLog.d("end event<------");
                        }
                    });
        }
    }